package com.cy.mall.service.comm.impl;

import java.io.InputStream;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.cy.mall.comm.constant.Constants;
import com.cy.mall.comm.utils.CommonUtils;
import com.cy.mall.model.Result;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.comm.FileUploadService;

import lombok.extern.slf4j.Slf4j;

/**
 * Oss上传文件service
 *
 * @author tanyilin
 *
 * @create 2019年2月27日 下午2:46:59
 */
@Slf4j
@Service
public class OssFileUploadServiceImpl extends BaseService implements FileUploadService {

	@Autowired
	private OSSClient ossClient;

	@Override
	public Result<Map<String, String>> uploadImage(MultipartFile multipartFile, String directory, String fileName) {

		long fileLength = multipartFile.getSize();
		if(fileLength > Constants.MAX_FILE_SIZE.longValue()) {
			return Result.fail("图片大小不可超过" + Constants.MAX_FILE_SIZE_MB + "MB");
		}
		
		boolean flag = false;
		String key = null;
		String bucketName = Constants.BUCKETNAME;
		
		try {
			
			InputStream inputStream = multipartFile.getInputStream();
			String originalFileName = multipartFile.getOriginalFilename();
			String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
			if(CommonUtils.isEmpty(fileName)) {
				//如果文件名为空，则生成一个唯一的文件名
				fileName = UUID.randomUUID().toString();
			}
			key = directory.concat(fileName).concat(suffix);
			log.info("OSS 上传图片 uploadImage:[{}]-->[{}]", fileName, key);
			log.info("图片大小为：{} ", fileLength/(1024 * 1024) == 0 ? fileLength/1024 + "Kb" : fileLength/(1024 * 1024) + "Mb");
			//上传
			ossClient.putObject(bucketName, key, inputStream);
			flag = true;
			
		} catch (Exception e) {
			log.error("uploadImage error 上传图片异常", e);
		}
		
		if(!flag) {
			return Result.fail("上传图片失败");
		}
		
		Map<String, String> map = new HashMap<>();
		map.put("path", key);
		map.put("src", getFileUrl(key, bucketName));
		
		return Result.success("上传图片成功", map);
	}

	@Override
	public String getFileUrl(String key, String bucketName) {
		
		URL url = null;
		Date expiration = CommonUtils.addTime(new Date(), 2, Calendar.HOUR_OF_DAY);
//		String suffix = key.substring(key.lastIndexOf("."));
//		if(Constants.IMAGE_SUFFIX.contains(suffix)) {
//			// 如果是可裁剪、缩放的图片
//			// 设置图片处理样式。
//			String style = "image/resize,p_50";
//			GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
//			req.setExpiration(expiration);
//			req.setProcess(style);
//			url = ossClient.generatePresignedUrl(req);
//
//		}else {
//			url = ossClient.generatePresignedUrl(bucketName, key, expiration);
//		}
		url = ossClient.generatePresignedUrl(bucketName, key, expiration);
		return url.toString();
	}
	
	
}
